﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

 
namespace mojoPortal.Web.RS.SBM
{
    public partial class RSViewInventoryPart : RSBasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitializeReadyServicePage("Parts Inventory");
                RSUserIdentity userIdentity = GetRSUserIdentity();
                int companyid = userIdentity.RSCompanyId;
                LoadInventoryParentCat(companyid);
                RSSBMDataLookupLoader.LoadInventoryCategoryDDL(companyid, ddlInventoryCategory);
            }
        }
        protected void ddlInventoryCategory_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            string sql_and = "";
            int isel = Convert.ToInt32(ddlInventoryCategory.SelectedValue);
            if (isel > 0)
                sql_and = "AND ProductId=" + isel;
            LoadInventoryParentCat(companyid, sql_and);
        }
        protected void gvInventoryItem_Edit(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int productid = Convert.ToInt32(e.CommandArgument.ToString());
            if (companyid > 0 && productid > 0)
                ucModalProductControl.ShowModal(companyid, productid, false);
        }

        protected void btnAddInventory_Click(Object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                Response.Redirect("~/RS/SBM/RSManageProduct.aspx?action=add&cat=part");
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
            
        }

        public void LoadInventoryParentCat(int companyid, string sql_and = "")
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "SELECT ProductId,ProductName,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder, ReorderLevel,ProductDesc," + 
                                "Manufacture,Discontinued,ModelNo,ProductCode,ParentId,SupplierID,CompanyId " +
                                " FROM rs_tblInventory where isequipment=0 and companyid=@companyid and (parentid is null or parentid='' or parentid=0) " + sql_and  + " order by productname";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvInventoryParentList.DataSource = ds;
            gvInventoryParentList.DataBind();
            myConnection.Close();

        }
        private DataSet GetChildListDataSet(int companyid, int productid)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "SELECT ProductId,ProductName,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder, ReorderLevel,ProductDesc," +
                                "Manufacture,Discontinued,ModelNo,ProductCode,ParentId,SupplierID,CompanyId " +
                                " FROM rs_tblInventory where isequipment=0 and parentid=@productid and companyid=@companyid order by productname";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@productid";
            param.Value = productid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            return ds;
        }
        protected void gvInventoryParentList_OnRowDataBound(Object sender, GridViewRowEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView gv = (GridView)e.Row.FindControl("gvInventoryChildList");
                if (null != gv && gv is GridView)
                {
                    DataRowView drv = (DataRowView)e.Row.DataItem;
                    int productid = Convert.ToInt32(drv["ProductId"]);
                    gv.DataSource = GetChildListDataSet(companyid, productid);
                    gv.DataBind();
                }                
            }

        }
    }
}